package com.example.shiwu.service;

import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.Alarm;
import com.example.shiwu.db.vo.AlarmVo;
import com.example.shiwu.db.dao.AlarmVoDao;
import com.example.shiwu.db.dao.AlarmDao;
import com.example.shiwu.manager.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;

public class AlarmService {

    private DatabaseHelper dbHelper;
    private AlarmDao<Alarm> AlarmDao;
    private AlarmVoDao<AlarmVo>  AlarmVoDao;
    private ArrayList<Alarm> listAlarm = new ArrayList<>();

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;



    /**获取单个数据
     * @param Alarm
     * @return
     */
    public List<Alarm> getAlarm(Alarm Alarm) {
        listAlarm = new ArrayList<>();
        AlarmDao = BaseDaoFactory.getInstance().createBaseDao(AlarmDao.class, Alarm.class);
        //AlarmVoDao = BaseDaoFactory.getInstance().createBaseDao(AlarmVoDao.class, AlarmVo.class);
        listAlarm.addAll(AlarmDao.query(Alarm));
        return listAlarm;
    }

    /**分页获取数据
     * @param Alarm
     * @return
     */
    public List<Alarm> getAlarmList(Alarm Alarm,Integer startIndex,Integer limit) {
        AlarmDao = BaseDaoFactory.getInstance().createBaseDao(AlarmDao.class, Alarm.class);
        listAlarm = new ArrayList<>();
        listAlarm.addAll(AlarmDao.query(Alarm,null,"create_time desc",null,startIndex,limit));
        return listAlarm;
    }

    public boolean save(Alarm Alarm) {
        AlarmDao = BaseDaoFactory.getInstance().createBaseDao(AlarmDao.class, Alarm.class);
        long size = AlarmDao.insert(Alarm);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public void saveOrUpdade(Alarm Alarm,Alarm where) {
        AlarmDao = BaseDaoFactory.getInstance().createBaseDao(AlarmDao.class, Alarm.class);

        long size = AlarmDao.update(Alarm,where);
        if(size == 0 ){
            AlarmDao.insert(Alarm);
        }
    }

    public boolean updade(Alarm Alarm,Alarm where) {
        AlarmDao = BaseDaoFactory.getInstance().createBaseDao(AlarmDao.class, Alarm.class);

        long size = AlarmDao.update(Alarm,where);
        if(size >0 ){
            return true;
        }
        return false;
    }

    public List<AlarmVo> getAll(Integer currentPage, Integer pageSize,Integer itemId) {
        AlarmVoDao = BaseDaoFactory.getInstance().createBaseDao(AlarmVoDao.class, AlarmVo.class);
        AlarmVo alarmVo = new AlarmVo();

        List<AlarmVo> lis = AlarmVoDao.query(alarmVo,"tb_alarm alarm left join tb_items ite on ite.id = alarm.item_id left join tb_device dev on dev.item_id = alarm.item_id ",
                new String[]{"alarm.id","alarm.status as status","alarm.alarm_msg as alarmMsg","ite.id as itemId","dev.sn","ite.name","alarm.create_time"},
                "ite.id =? ",new String[]{String.valueOf(itemId)},"alarm.id","alarm.id"+" DESC","",currentPage,pageSize);
        return lis;
    }
}
